package cn.fintecher.pangolin.service.manage.web;

import cn.fintecher.pangolin.common.enums.StatusType;
import cn.fintecher.pangolin.common.web.BaseController;
import cn.fintecher.pangolin.entity.mysql.StatusConfig;
import cn.fintecher.pangolin.service.manage.request.AddConfigRequest;
import cn.fintecher.pangolin.service.manage.request.ModifyConfigRequest;
import cn.fintecher.pangolin.service.manage.response.StatusConfigResponse;
import cn.fintecher.pangolin.service.manage.service.StatusConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @Auther: xiaqun
 * @Description: 案件状态设置相关接口
 * @Date: 9:53 2019/9/7
 */

@RestController
@RequestMapping("/api/statusConfigController")
@Api(value = "案件状态设置相关接口", description = "案件状态设置相关接口")
public class StatusConfigController extends BaseController {
    @Autowired
    private StatusConfigService statusConfigService;

    /**
     * 新增配置
     */
    @ApiOperation(value = "新增配置", notes = "新增配置")
    @PostMapping("/addConfig")
    public ResponseEntity<Void> addConfig(@Valid @RequestBody AddConfigRequest request) {
        log.info("REST request to add config,{}", request);
        statusConfigService.addConfig(request);
        return ResponseEntity.ok().build();
    }

    /**
     * 修改配置
     */
    @ApiOperation(value = "修改配置", notes = "修改配置")
    @PutMapping("/modifyConfig")
    public ResponseEntity<Void> modifyConfig(@Valid @RequestBody ModifyConfigRequest request) {
        log.info("REST request to modify config,{}", request);
        statusConfigService.modifyConfig(request);
        return ResponseEntity.ok().build();
    }

    /**
     * 删除配置
     */
    @ApiOperation(value = "删除配置", notes = "删除配置")
    @DeleteMapping("/deleteConfig")
    public ResponseEntity<Void> deleteConfig(@RequestParam @ApiParam(value = "主键ID", required = true) String id) {
        log.info("REST request to delete config,{}", id);
        statusConfigService.deleteConfig(id);
        return ResponseEntity.ok().build();
    }

    /**
     * 查询全部配置
     */
    @ApiOperation(value = "查询全部配置", notes = "查询全部配置")
    @GetMapping("/findAllConfig")
    public ResponseEntity<Page<StatusConfig>> findAllConfig(@RequestParam(required = false) @ApiParam(value = "类型") StatusType type,
                                                            @RequestParam(required = false) @ApiParam(value = "委托方ID") String principalId,
                                                            Pageable pageable,
                                                            @RequestHeader(value = "X-UserToken") String token) {
        log.info("REST request to find all config,{},{}", type, principalId);
        Page<StatusConfig> page = statusConfigService.findAllConfig(type, principalId, pageable, token);
        return ResponseEntity.ok().body(page);
    }

    /**
     * 不分页查询全部配置
     */
    @ApiOperation(value = "不分页查询全部配置", notes = "不分页查询全部配置")
    @GetMapping("/findAllConfigNoPage")
    public ResponseEntity<List<StatusConfig>> findAllConfigNoPage(@RequestParam(required = false) @ApiParam(value = "类型") StatusType type,
                                                                  @RequestHeader(value = "X-UserToken") String token) {
        log.info("REST request to find all config no page,{}", type);
        List<StatusConfig> list = statusConfigService.findAllConfigNoPage(type, token);
        return ResponseEntity.ok().body(list);
    }

    /**
     * 获取手工状态
     */
    @ApiOperation(value = "获取手工状态", notes = "获取手工状态")
    @GetMapping("/getStatusConfig")
    public ResponseEntity<StatusConfigResponse> getStatusConfig(@RequestParam @ApiParam(value = "委托方ID", required = true) String principalId) {
        log.info("REST request to get status config,{}", principalId);
        StatusConfigResponse response = statusConfigService.getStatusConfig(principalId);
        return ResponseEntity.ok().body(response);
    }
}